﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once

#include "GmPositioningElements.h"
#include "IGridNew.h"
#include "DbDataSchema.h"
#include "RegenDataSchema.h"
#include "SurfaceAndTerminals.h"
#include "Leaders.h"
#include "ICurve2d.h"
#include "Matrix4d.h"
#include "ElementCreationOptions.h"
#include "IElementDeletionComponent.h"

namespace gcmp 
{
    class IElement;
    struct GridNewInput;
    
    class GM_POSITIONING_ELEMENTS_EXPORT SingleGrid : public NdbObjectSchematic, public IGridNew
    {
        CANNOT_COPY_OR_ASSIGN(SingleGrid)

        DBOBJECT_DATA_BEGIN_AND_QUICK_CAST_FROM(gcmp, SingleGrid, gcmp::NdbObjectSchematic, E2767678 - 0DA3 - 455E - 808A - 0D05D0B826D6, gmpositioningelements, IGridNew)
            DATA_TRANSIENT(IElement*, pOwnerElement)
            DATA_REGEN(OwnerPtr<SurfaceAndTerminals>, SurfaceAndTerminals) // 基准面和边界
            DATA_REGEN(OwnerPtr<Leaders>, Leaders) // 弯头
            DATA(Bool, StartTagVisible) // 平面视图的起点标头可见性
            DATA(Bool, EndTagVisible) // 平面视图的终点标头可见性
        DBOBJECT_DATA_END
    public:
        virtual ~SingleGrid() {}

    public:
        virtual bool SetOwnerElement(IElement* pOwnerElement) override;

        virtual const IElement* GetOwnerElement() const override { return m_pOwnerElement; }

        virtual IElement* GetOwnerElement() override { return m_pOwnerElement; }

        virtual NdbObject* GetTopOwnerObject() const override;

        IDocument* GetDocument() const;

        ElementId GetOwnerElementId() const;

#pragma region 关联更新
        virtual void UpdateForWeakParentDeletion(const std::set<ElementId>& deletedElementIds) override;

        virtual void ReportParents(IElementParentReporter& reporter) const override;

#pragma endregion 关联更新

#pragma region 其他
        virtual bool FinishEditing() override;

#pragma endregion 其他
    public:
        static IGridNew* Create(const GridNewInput& gridNewInput);
    private:
        static IGridNew* CreateAsEmpty(
            IDocument* pDoc,
            ElementCreationOptions eco = ElementCreationOptions::Normal);

        static IGridNew* CreateAsLine(
            IDocument* pDoc,
            const ILine2d* pLine2d,
            const Intervald& verticalExtents,
            ElementCreationOptions eco = ElementCreationOptions::Normal);

        static IGridNew* CreateAsArc(
            IDocument* pDoc,
            const IArc2d* pArc2d,
            const Intervald& verticalExtents,
            ElementCreationOptions eco = ElementCreationOptions::Normal);

#pragma region IGridNew接口
    public:
        virtual std::vector<OwnerPtr<ISurface>> GetGridSurfaces(ElementId modelviewId = ElementId::InvalidID) const override final;

        virtual std::vector<OwnerPtr<ICurve3d>> GetHorizontalGridCurves(ElementId modelviewId = ElementId::InvalidID) const override final;
        virtual OwnerPtr<ICurve3d> GetVerticalGridCurve(ElementId modelviewId = ElementId::InvalidID) const override final;

        virtual Vector3d GetVerticalDirection() const override final; 

        virtual bool SetHorizontalGridCurves(const std::vector<const ICurve2d*>& curves) override final;
        virtual bool SetVerticalExtents(const Intervald& extents, ElementId modelviewId = ElementId::InvalidID) override final;

        virtual OwnerPtr<IGraphicsElementShape> CreateElementShapeBasedOnPlane(const IPlane* pPlane, double scale, const IModelView* pModelView) const override final;

        virtual std::wstring GetName() const override final;
        virtual bool SetName(const std::wstring& name, std::wstring* errorMessage = nullptr) override final;

        // 起点终点的轴网标签是否可见
        virtual bool IsStartTagVisibleInPlanView() const override { return this->GetStartTagVisible__(); }
        virtual bool SetStartTagVisibleInPlanView(bool visible) override { this->SetStartTagVisible__(visible); return true; }
        virtual bool IsEndTagVisibleInPlanView() const override { return this->GetEndTagVisible__(); }
        virtual bool SetEndTagVisibleInPlanView(bool visible) override { this->SetEndTagVisible__(visible); return true; }
        // 轴网在水平面上是否是直线or圆弧
        virtual bool IsHorizontalStraight() const override;
        virtual bool IsHorizontalArc() const override;
        //自动命名
        virtual bool AutoName() override;

#pragma endregion IGridNew接口

    public:
        // 轴网在水平面上的起点终点位置和切线方向
        Vector3d GetHorizontalStartPoint(ElementId modelviewId = ElementId::InvalidID) const;
        bool SetHorizontalStartPoint(const Vector3d& point, ElementId modelviewId = ElementId::InvalidID);

        Vector3d GetHorizontalEndPoint(ElementId modelviewId = ElementId::InvalidID) const;
        bool SetHorizontalEndPoint(const Vector3d& point, ElementId modelviewId = ElementId::InvalidID);

        Vector3d GetHorizontalStartTangent(ElementId modelviewId = ElementId::InvalidID) const;
        Vector3d GetHorizontalEndTangent(ElementId modelviewId = ElementId::InvalidID) const;

        // 轴网在竖直面上的起点终点位置和切线方向
        Vector3d GetVerticalStartPoint(ElementId modelviewId = ElementId::InvalidID) const;
        bool SetVerticalStartPoint(const Vector3d& point, ElementId modelviewId = ElementId::InvalidID);

        Vector3d GetVerticalEndPoint(ElementId modelviewId = ElementId::InvalidID) const;
        bool SetVerticalEndPoint(const Vector3d& point, ElementId modelviewId = ElementId::InvalidID);

        Vector3d GetVerticalStartTangent(ElementId modelviewId = ElementId::InvalidID) const;
        Vector3d GetVerticalEndTangent(ElementId modelviewId = ElementId::InvalidID) const;

        // 轴网在竖直方向上的延伸范围
        Intervald GetVerticalExtends(ElementId modelviewId = ElementId::InvalidID) const;

        // 轴网是否被任何视图重载范围
        bool IsOverridedByAnyModelView() const;

        // 清除该轴网被所有视图重载的范围
        bool ClearModelViewOverridedParams();

        // 变换
        bool Transform(const Matrix4d& mat);

        static gcmp::UniIdentity GetImplementationUid();

    private:
        // 获取轴网端点
        enum EnTerminalType { TT_Start, TT_End };
        Vector3d GetTerminalPoint(EnTerminalType uTerminalType, EnTerminalType vTerminalType, ElementId modelviewId = ElementId::InvalidID) const;

        // 根据给定的二维曲线，修改轴网的水平向几何
        bool EditAsHorizontalCurve(const ICurve2d* pCurve2d, ElementId modelviewId);
    };

}// namespace gcmp
